import { VideoService } from '../services/videoService'
import { PLAYER } from '../constants/player'
import { getFileName, addSafeFileProtocol } from '../utils/file'
import { handleError, createErrorMessage } from '../utils/error'
import { useVideoStore } from '../renderer/stores/videoStore'

const remote = window.require('@electron/remote')
const { dialog } = remote

export function useFileDialog() {
  const videoStore = useVideoStore()

  const openVideoFile = async () => {
    try {
      const result = await dialog.showOpenDialog({
        properties: ['openFile'],
        filters: [
          { name: '视频文件', extensions: PLAYER.VIDEO.EXTENSIONS }
        ]
      })

      if (!result.canceled && result.filePaths.length > 0) {
        const filePath = result.filePaths[0]
        const name = getFileName(filePath)
        
        await VideoService.createOrUpdateVideo(filePath, name)
        
        videoStore.currentVideo = {
          path: addSafeFileProtocol(filePath),
          name,
          originalPath: filePath
        }
        await videoStore.loadTags(filePath)
        videoStore.setCurrentPage('player')
      }
    } catch (error) {
      console.error(createErrorMessage('打开文件对话框', error))
    }
  }

  return {
    openVideoFile
  }
} 